import type { Context } from 'koa';
import { ActivityService } from '@/services/activity';
import { ChatService } from '@/services/chat';
import { ActivityCreateDto } from '@/dtos/activityCreate.dto';

// 注册
export const activityCreateController = async (ctx: Context) => {
  const uid = ctx.userId;
  const input = ctx.request.body;
  const params = await ctx.vailParams<ActivityCreateDto>(ActivityCreateDto, input);

  // 先创建一个聊天室, 并把自己加入到聊天室中
  const chatRoom = await ChatService.createNewChatRoom({
    creatorId: uid,
    users: [uid]
  });

  // 创建一个活动, 与创建的聊天室进行绑定
  const activity = await ActivityService.createNewActivity({
    ...params,
    creator: uid,
    chatRoom: chatRoom._id
  });

  const activityId = activity._id;

  // 创建完活动以后把自己放入到活动中
  await ActivityService.userParticipateActivity({
    activityId,
    userId: uid
  });

  ctx.sendSuccess({ id: activityId });
}